{% extends 'base.html' %}
{% load static %}
{% load i18n %}

{% block custom_head_css_js %}
    <link href="{% static 'css/plugins/select2/select2.min.css' %}" rel="stylesheet">
    <link href="{% static "css/plugins/sweetalert/sweetalert.css" %}" rel="stylesheet">
    <link href="{% static "css/plugins/datatables/datatables.min.css" %}" rel="stylesheet">
    <link href="{% static "css/plugins/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css" %}" rel="stylesheet">
    <script src="{% static 'js/plugins/select2/select2.full.min.js' %}"></script>
    <script src="{% static "js/plugins/sweetalert/sweetalert.min.js" %}"></script>
    <script src="{% static "js/plugins/datatables/datatables.min.js" %}"></script>
{% endblock %}
{% block content %}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="panel-options">
                        <ul class="nav nav-tabs">
                            <li class="active">
                                <a href="{% url 'users:user-group-detail' pk=user_group.id %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'User group detail' %} </a>
                            </li>
                            <li>
                                <a href="{% url 'users:user-group-granted-asset' pk=user_group.id %}" class="text-center"><i class="fa fa-cubes"></i> {% trans 'Asset granted' %}</a>
                            </li>
                            <li class="pull-right">
                                <a class="btn btn-outline btn-default" href="{% url 'users:user-group-update' pk=user_group.id %}"><i class="fa fa-edit"></i>{% trans 'Update' %}</a>
                            </li>
                            <li class="pull-right">
                                <a class="btn btn-outline btn-danger btn-delete-user-group">
                                    <i class="fa fa-trash-o"></i>{% trans 'Delete' %}
                                </a>
                            </li>
                        </ul>
                    </div>
                    <div class="tab-content">
                        <div class="col-sm-7" style="padding-left: 0">
                            <div class="ibox float-e-margins">
                                <div class="ibox-title">
                                    <span class="label"><b>{{ user_group.name }}</b></span>
                                    <div class="ibox-tools">
                                        <a class="collapse-link">
                                            <i class="fa fa-chevron-up"></i>
                                        </a>
                                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                            <i class="fa fa-wrench"></i>
                                        </a>
                                    </div>
                                </div>
                                <div class="ibox-content">
                                    <table class="table">
                                        <tbody>
                                        <tr class="no-borders-tr">
                                            <td width="20%">{% trans 'Name' %}:</td>
                                            <td><b>{{ user_group.name }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Create by' %}:</td>
                                            <td><b>{{ user_group.created_by }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Date created' %}:</td>
                                            <td><b>{{ user_group.date_created }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Comment' %}:</td>
                                            <td><b>{{ user_group.comment }}</b></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-5" style="padding-left: 0;padding-right: 0">
                            <div class="panel panel-primary">
                                <div class="panel-heading">
                                    <i class="fa fa-info-circle"></i> {% trans 'User' %}
                                </div>
                                <div class="panel-body">
                                    <table class="table user_edit">
                                        <tbody>
                                        <form>
                                            <tr>
                                                <td colspan="2" class="no-borders">
                                                    <select data-placeholder="{% trans 'Add user' %}" id="slct_users" class="select2" style="width: 100%" multiple="" tabindex="4">
                                                        {% for user in users %}
                                                        <option value="{{ user.id }}" id="opt_{{ user.id }}">{{ user.name }}</option>
                                                        {% endfor %}
                                                    </select>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="2" class="no-borders">
                                                    <button type="button" class="btn btn-primary btn-small" id="btn_add_user">{% trans 'Add' %}</button>
                                                </td>
                                            </tr>
                                        </form>

                                        {% for user in user_group.users.all %}
                                        <tr>
                                          <td ><b class="bdg_user" data-uid={{ user.id }}>{{ user.name }}</b></td>
                                          <td>
                                              <button class="btn btn-danger pull-right btn-xs btn_remove_user" type="button"><i class="fa fa-minus"></i></button>
                                          </td>
                                        </tr>
                                        {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block custom_foot_js %}
<script>
jumpserver.users_selected = {};

function updateGroupMember(users) {
    var the_url = "{% url 'api-users:user-group-update-user' pk=user_group.id %}";
    var body = {
        users: Object.assign([], users)
    };
    var success = function(data) {
        // remove all the selected groups from select > option and rendered ul element;
        $('.select2-selection__rendered').empty();
        $('#slct_users').val('');
        $.map(jumpserver.users_selected, function(user_name, index) {
            $('#opt_' + index).remove();
            // change tr html of users
            $('.user_edit tbody').append(
                '<tr>' +
                '<td><b class="bdg_user" data-uid="' + index + '">' + user_name + '</b></td>' +
                '<td><button class="btn btn-danger btn-xs pull-right btn_remove_user" type="button"><i class="fa fa-minus"></i></button></td>' +
                '</tr>'
            )
        });
        // clear jumpserver.selected_groups
        jumpserver.users_selected = {};
    };
    APIUpdateAttr({
        url: the_url,
        body: JSON.stringify(body),
        success: success
    });
}

$(document).ready(function () {
   $('.select2').select2()
          .on('select2:select', function(evt) {
            var data = evt.params.data;
            jumpserver.users_selected[data.id] = data.text;
        }).on('select2:unselect', function(evt) {
            var data = evt.params.data;
            delete jumpserver.users_selected[data.id]
        })
}).on('click', '.btn_remove_user', function() {
    var $this = $(this);
    var $tr = $this.closest('tr');
    var $badge = $tr.find('.bdg_user');
    var uid = $badge.data('uid');
    var user_name = $badge.html() || $badge.text();
    $('#slct_users').append(
        '<option value="' + uid + '" id="opt_' + uid + '">' + user_name + '</option>'
    );
    $tr.remove();
    var users = $('.bdg_user').map(function() {
        return $(this).data('uid');
    }).get();
    updateGroupMember(users)
}).on('click', '#btn_add_user', function() {
    if (Object.keys(jumpserver.users_selected).length === 0) {
        return false;
    }
    var users = $('.bdg_user').map(function() {
        return $(this).data('uid');
    }).get();
    $.map(jumpserver.users_selected, function(value, index) {
        users.push(index);
        $('#opt_' + index).remove();
    });
    updateGroupMember(users)
}).on('click', '.btn-delete-user-group', function () {
    var $this = $(this);
    var name = "{{ user_group.name }}";
    var uid = "{{ user_group.id }}";
    var the_url = '{% url "api-users:user-group-detail" pk=DEFAULT_PK %}'.replace('{{ DEFAULT_PK }}', uid);
    var redirect_url = "{% url 'users:user-group-list' %}";
    objectDelete($this, name, the_url, redirect_url);
})
</script>
{% endblock %}
